package com.jgy.controller;

import com.jgy.annotation.SetLogValue;
import com.jgy.entity.SpaceUser;
import com.jgy.mapper.SpaceUserMapper;
import com.jgy.utils.CommonConstants;
import com.jgy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
public class LoginController {

    @Autowired
    private SpaceUserMapper mapper;

    @SetLogValue(modular = "测试接口login-success",type = CommonConstants.SELECT, describe = "测试接口login-success")
    @RequestMapping("/login-success")
    public String loginSuccess() {

        return "登录成功";
    }
    @SetLogValue(modular = "测试接口getUser",type = CommonConstants.SELECT, describe = "测试接口getUser")
    @GetMapping("/getuser")
    public R getUser(){
        List<SpaceUser> spaceUsers = mapper.selectList(null);
        List<SpaceUser> collect = spaceUsers.stream().filter(data -> data.getId() != null).collect(Collectors.toList());
        return R.ok(collect);
    }

    @SetLogValue(modular = "测试接口r/r1",type = CommonConstants.SELECT, describe = "测试接口r/r1")
    @RequestMapping("/r/r1")
    @PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问
    public String r1() {
        return "访问r1资源";
    }
    @SetLogValue(modular = "测试接口r/r2",type = CommonConstants.SELECT, describe = "测试接口r/r2")
    @RequestMapping("/r/r2")
    @PreAuthorize("hasAuthority('p2')")//拥有p2权限方可访问
    public String r2() {
        return "访问r2资源";
    }



}
